<?php

namespace addons\csmitsm\library\xcore\xcflow\utils\xcformfields\xcform;

use addons\csmitsm\library\xcore\xcflow\utils\xcformfields\xcform\base\XcflowAField;
use addons\csmitsm\library\xcore\xcore\utils\XcDaoUtils;

class xcform_control_value_autono extends XcflowAField
{
    public function getDbfieldColumnType()
    {
        return "varchar(255)";
    }

    public function assertXcfieldjson()
    {
        $json = $this->fieldjsonobj;
        if (empty($json['xcform_base_inputfieldcode']['value'])) {
            $this->assertRequired("字段编码");
        }
        if (empty($json['xcform_base_xctitle']['value'])) {
            $this->assertRequired("字段名称");
        }
    }


    public function pagevalueToDbvalue($pagevalue)
    {
        return $pagevalue;
    }

 

    public function dbvalueToPagevalue($dbvalue)
    {
        return $dbvalue;
    }

    public function processSubmitPageValue($xcfieldjsonObj) {}
    public function processSavePageValue($xcfieldjsonObj) {}

    // 非abstract method,仅在XcformADataCrudBackend submit 时调用
    public function generateDbValue()
    {
        $no = "";
        foreach ($this->fieldjsonobj['xcform_base_property_value_autono']['rules'] as $rule) {
            switch ($rule['type']) {
                case "constant_string":
                    $no .= $rule['constant_string'];
                    break;
                case "create_date":
                    $no .= date($rule['create_date_format']);
                    break;
                case "auto_num":
                    // if null then begin_count , else from fa_csmitsm_xcflowform_autono
                    $dao = new \app\admin\model\csmitsm\XcflowformAutono();
                    $row = XcDaoUtils::getRowBySql($dao, [
                        ['xcformtype', '=', $this->xcfieldjsonObj['xcformtype']],
                        ['xcform_id', '=', $this->xcfieldjsonObj['xcform_id']]
                    ]);

                    $isnew = true;
                    $year =  date("Y");
                    $month =  date("m");
                    $date =  date("d");

                    if (!$row) {
                        $isnew = true;
                    } else {

                        switch ($rule['recount_type']) {
                            case "no":
                                $isnew = false;
                                break;
                            case "per_day":
                                $isnew = ($row['year'] . $row['month'] . $row['date'] == $year . $month . $date) ? false : true;
                                break;
                            case "per_month":
                                $isnew = ($row['year'] . $row['month'] == $year . $month) ? false : true;
                                break;
                            case "per_year":
                                $isnew = ($row['year']  == $year) ? false : true;
                                break;
                        }
                    }
                    if ($isnew) {
                        $currentnum = $rule['number_of_digit'];
                    } else {
                        $currentnum = $row->currentnum + 1;
                    }

                    $model = [
                        'xcformtype' => $this->xcfieldjsonObj['xcformtype'],
                        'xcform_id' => $this->xcfieldjsonObj['xcform_id'],
                        'year' => $year,
                        'month' => $month,
                        'date' => $date,
                        'currentnum' => $currentnum,
                        'createtime' => time(),
                        'weigh' => 0,
                    ];
                    if ($row) {
                        XcDaoUtils::save($dao, $row->id, $model);
                    } else {
                        XcDaoUtils::create($dao, $model);
                    }
                    $no .= str_pad($currentnum, $rule['number_of_digit'], '0', STR_PAD_LEFT);
            }
        }
        return $no;
    }
}
